const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            photo,
            article_info: [],
            article_specifications: [],
            fileList:[],
            childDataForm2:[]
        };
    },
    mounted() {
        this.getCategory();
    },
    methods: {
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'article_info',
                    Condition: "Row_Status<>'1'",
                },
                success: (data) => {
                    console.log(data);
                    this.article_info = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        changeArticle(val) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'article_specifications',
                    Condition: "Row_Status='0' AND Article_SerialNumber='" + val + "'",
                },
                success: (data) => {
                    console.log(data);
                    this.article_specifications = data;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            })
        },
        success(row, res,Attachment_Type) {
            this.childDataForm2.push({
                uid: row.uid,
                Article_Inventory_Enter_SerialNumber:this.dataForm.Article_Inventory_Enter_SerialNumber,
                Attachment_URL: res.fileName,
                Attachment_Name:row.name,
                Temp_Path:res.tempPath,
                Attachment_Type
            })
        },
        remove(row) {
            if (!Object.keys(row).some(item => item.includes('_Original'))) {
                this.childDataForm2 = this.childDataForm2.filter(i => i.uid != row.uid);
            } else {
                let obj = this.childDataForm2.filter(i => i.Attachment_SerialNumber == row.Attachment_SerialNumber)[0];
                obj.Row_Status = '1'
            }
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                let ls_primary_key = lJSONObject_Data_Options.primary_key;

                this[ls_DataForm].Article_Inventory_SerialNumber = this[ls_DataForm].Article_SerialNumber + '_' + this[ls_DataForm].Specifications_SerialNumber;
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                        if (item.hasAttribute('data-options')) {
                            let lJSONObject_ChildData_Options = new Object();
                            lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                            let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                            let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                            if (this[ls_DataForm][ls_primary_key]) {
                                this[ls_ChildDataForm].forEach(item => {
                                    item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
                                })
                            }
                            //建立子表结构体
                            let JSONArray_ChildRecord = new Array();
                            JSONArray_ChildRecord = this[ls_ChildDataForm];
                            if (!this[ls_DataForm].Children) {
                                this[ls_DataForm].Children = new Object();
                            }
                            this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                        }
                    })
                } catch (error) {

                }
                //建立新增提交结构
                let lJSONObject_DataSet = new Object();

                if (this.nodeKey) {
                    this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                }
                let lJSONArray_Record = new Array();
                lJSONArray_Record.push(this[ls_DataForm]);
                let lJSONObject_Table = new Object();
                lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                lJSONObject_DataSet[action] = lJSONObject_Table;

                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: 'article_inventory',
                        Condition: `Article_Inventory_SerialNumber='${this[ls_DataForm].Article_Inventory_SerialNumber}'`,
                    },
                    success: (data) => {
                        let Inventory_Enter_Quantity = 0;
                        this.childDataForm.forEach(item=>{
                            Inventory_Enter_Quantity = accAdd(Inventory_Enter_Quantity, item.Inventory_Enter_Quantity);
                        }) 
                        if (data.length == 1) {
                            data[0].Inventory_Enter_Quantity = accAdd(data[0].Inventory_Enter_Quantity, Inventory_Enter_Quantity);
                            data[0].Inventory_Quantity = accAdd(data[0].Inventory_Quantity, Inventory_Enter_Quantity);
                            lJSONObject_DataSet['Modify'] = {
                                article_inventory:data
                            }
                        } else {
                            data.push({
                                Article_Inventory_SerialNumber:this[ls_DataForm].Article_Inventory_SerialNumber,
                                Article_SerialNumber:this[ls_DataForm].Article_SerialNumber,
                                Specifications_SerialNumber:this[ls_DataForm].Specifications_SerialNumber,
                                Inventory_Enter_Quantity,
                                Inventory_Quantity:Inventory_Enter_Quantity
                            })
                            lJSONObject_DataSet['Add']['article_inventory'] = data;
                        }
                        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                        lJSONObject_DataSet['Add']['base_operation_log'] = [{
                            Operation_Type: '0',
                            Operation_Path: this.relativePath,
                            Operation_Main,
                            Operation_Interface: "generalSaveByRequestBody",
                            Operation_IP: getIpAddress()
                        }]
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                window.location.href = './index.html'
                            }
                            this.loading = false;
                        })
                        // $.ajax({
                        //     headers: requestHeaders("JSON"),
                        //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                        //     method: "post",
                        //     data: JSON.stringify({
                        //         Data: lJSONObject_DataSet,
                        //     }),
                        //     success: (res) => {
                        //         if (res.Status == 'success') {
                        //             window.location.href = './index.html'
                        //         }
                        //     },
                        //     complete: (XMLHttpRequest) => {
                        //         this.loading = false;
                        //         requestPretreatment(XMLHttpRequest)
                        //     }
                        // });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader)
app.component('yl-text-editor', ylTextEditor);
app.component('yl-select', ylSelect)
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
